package edu.jd.block_chain.forum.inChat.impl;

import edu.jd.block_chain.forum.inChat.HostChat;
import edu.jd.block_chain.forum.inChat.InChatDto;
import edu.jd.block_chain.forum.inChat.InChatService;
import edu.jd.block_chain.forum.inChat.dao.InChatDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @descriptions:
 * @data: 2021/11/26 16:36
 */
@Service
@Transactional
public class InChatServiceImpl implements InChatService {
    @Resource
    private InChatDao inChatDao;

    @Override
    public List<HostChat> getHostChat(Integer p_id) {
        List<HostChat> hostChats = inChatDao.getHostChatList(p_id);
        for (HostChat hostChat : hostChats) {
            hostChat.setChildren(inChatDao.getSubChatListByHostId(hostChat.getIn_c_host_id()));
        }
        return hostChats;
    }

    @Override
    public void addHostChat(InChatDto dto) {
        Integer hostId = inChatDao.getHostId(dto.getP_id());
        dto.setIn_c_host_id(hostId==null?0:hostId);
        System.out.println(dto.getIn_c_host_id());
        inChatDao.addHostChat(dto);
    }

    @Override
    public void addSubChat(InChatDto dto) {
        System.out.println(dto.getIn_c_host_id());
        Integer subId = inChatDao.getSubId(dto.getP_id(),dto.getIn_c_host_id());
        dto.setIn_c_sub_id(subId==null?0:subId);
        inChatDao.addSubChat(dto);
    }
}
